package me.itsoo.artemis.framework.ioc.bean;

import me.itsoo.artemis.framework.core.filter.BiFilter;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

/**
 * BeanMatchPatterns
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/6/8
 */
public class BeanMatchPatterns implements BiFilter<String, String> {

    private final Map<String, Pattern> patternCache = new ConcurrentHashMap<>();

    @Override
    public boolean match(String regex, String beanName) {
        Pattern pattern = patternCache.get(regex);
        if (Objects.isNull(pattern)) {
            pattern = Pattern.compile(regex);
            patternCache.putIfAbsent(regex, pattern);
        }

        return pattern.matcher(beanName).matches();
    }
}
